package chaw.android.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeCheck {
	
	public static String checkTime(String time){
		SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss", Locale.KOREA );
		Date currentTime = new Date ( );
		String dTime = formatter.format ( currentTime );
		
		String[] values = time.split(" ");
		String[] days = values[0].split("-");
		String[] times = values[1].split(":");
		
		String[] nvalues = dTime.split(" ");
		String[] ndays = nvalues[0].split("-");
		String[] ntimes = nvalues[1].split(":");
		
		String date;
		
		int year = new Integer(days[0]);
		int month = new Integer(days[1]);
		int day = new Integer(days[2]);
		
		int hour = new Integer(times[0]);
		int min = new Integer(times[1]);
		
		int nyear = new Integer(ndays[0]);
		int nmonth = new Integer(ndays[1]);
		int nday = new Integer(ndays[2]);

		int nhour = new Integer(ntimes[0]);
		int nmin = new Integer(ntimes[1]);
		
		if( nyear != year ){
			if(nyear - year == 1 && nmonth < month){
				date = "" + (12 - month + nmonth) + "개월전";
			}
			else{
				date = "" + (nyear - year) + "년전";
			}
		}
		else if( nmonth != month ){
			if(nmonth - month == 1 && nday < day){
				date = "" + (getDate(month) - day + nday) + "일전";
			}
			else{
				date = "" + (nmonth - month) + "개월전";
			}
		}
		else if( nday != day ){
			if(nday - day == 1 && nhour < hour){
				date = "" + (24 - hour + nhour) + "시간전";
			}
			else{
				date = "" + (nday - day) + "일전";
			}
		}
		else if( nhour != hour ){
			if(nhour - hour == 1 && nmin < min){
				date = "" + (60 - min + nmin) + "분전";
			}
			else{
				date = "" + (nhour - hour) + "시간전";
			}
		}
		else if( nmin != min ){
			date = "" + (nmin - min) + "분전";
		}
		else{
			date = "방금";
		}
		
		return date;
	}
	
	public static int getDate(int month){
		int day = 0;
		switch(month){
		case 1:
			day = 31;
			break;
		case 2:
			day = 28;
			break;
		case 3:
			day = 31;
			break;
		case 4:
			day = 30;
			break;
		case 5:
			day = 31;
			break;
		case 6:
			day = 30;
			break;
		case 7:
			day = 31;
			break;
		case 8:
			day = 31;
			break;
		case 9:
			day = 30;
			break;
		case 10:
			day = 31;
			break;
		case 11:
			day = 30;
			break;
		case 12:
			day = 31;	
			break;
		}
		return day;
	}

}
